package com.llj.Road_curve_measuring_system.module.results;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import com.llj.Road_curve_measuring_system.R;
import com.llj.Road_curve_measuring_system.base.BaseActivity;
import com.llj.Road_curve_measuring_system.bean.ScreenResult;
import com.llj.Road_curve_measuring_system.common.GlobalConstant;
import com.llj.Road_curve_measuring_system.databinding.ActivityResultCircularCurveDmBinding;
import com.llj.Road_curve_measuring_system.databinding.ActivityResultCircularCurveSmBinding;
import com.llj.Road_curve_measuring_system.databinding.ActivityResultModerationCurveDmBinding;
import com.llj.Road_curve_measuring_system.databinding.ActivityResultModerationCurveSmBinding;
import com.llj.Road_curve_measuring_system.module.input.InputActivity;
import com.llj.Road_curve_measuring_system.module.input.InputModel;
import com.llj.Road_curve_measuring_system.util.ActivityUtil;
import com.llj.Road_curve_measuring_system.util.MyUtil;
import com.llj.Road_curve_measuring_system.util.ToastUtil;

import java.util.List;
import java.util.function.ToDoubleBiFunction;

public class ResultActivity extends BaseActivity<ResultPresenter> implements ResultView{

    //对应四个布局
    protected ActivityResultCircularCurveDmBinding ccDmBinding;
    protected ActivityResultModerationCurveDmBinding mcDmBinding;
    protected ActivityResultCircularCurveSmBinding ccSmBinding;
    protected ActivityResultModerationCurveSmBinding mcSmBinding;

    String data1 ;
    String data2 ;
    int path;


    @Override
    protected ResultPresenter createPresenter() {
        return new ResultPresenter(this);
    }

    @Override
    protected void initView() {
        path = ActivityUtil.getIntentThirdData();
        if (data1.equals(GlobalConstant.CIRCULAR_CURVE)){
            if (data2.equals(GlobalConstant.DECLINATION_METHOD)){
                if (path != 0){
                    ccDmBinding.save.setVisibility(View.INVISIBLE);
                }
                initCcDmTable(new String[]{"id","licheng","interval","pianjiao"},InputModel.resList);
                ccDmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.CCDM,this));
            }else if (data2.equals(GlobalConstant.TANGENT_SUPPORT_METHOD)){
                if (path != 0){
                    ccSmBinding.save.setVisibility(View.INVISIBLE);
                }
                initCcSmTable(new String[]{"id","quxianchang","x","y"},InputModel.yqxresList);
                ccSmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.CCTS,this));
            }else {
                if (path != 0){
                    ccDmBinding.save.setVisibility(View.INVISIBLE);
                }
                if (data2.equals(GlobalConstant.POLAR_COORDINATE_METHOD)){
                    initCcPVTable(new String[]{"id","licheng","quxianchang","pianjiao","x","y"},InputModel.yjiList,GlobalConstant.CCPV);
                    ccDmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.CCPV,this));
                }else {
                    // TODO: 2022/5/14   复用initCcTable方法,就改参数就行
                    initCcPVTable(new String[]{"id","x","y"},InputModel.ycsList,GlobalConstant.CCCS);
                    ccDmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.CCCS,this));
                }
            }
        }else {
            if (data2.equals(GlobalConstant.DECLINATION_METHOD)){
                if (path != 0){
                    mcDmBinding.save.setVisibility(View.INVISIBLE);
                }
                initMcDmTable(new String[]{"id","licheng","interval","pianjiao"},InputModel.hpjList);
                mcDmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.MCDM,this));
            }else if (data2.equals(GlobalConstant.TANGENT_SUPPORT_METHOD)){
                if (path != 0){
                    mcSmBinding.save.setVisibility(View.INVISIBLE);
                }
                // 载入对应的initTable方法
                initMcSmTable(new String[]{"id","licheng","quxianchang","x","y"},InputModel.hqxList);
                mcSmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.MCTS,this));
            }else {
                if (path != 0){
                    mcDmBinding.save.setVisibility(View.INVISIBLE);
                }
                if (data2.equals(GlobalConstant.POLAR_COORDINATE_METHOD)){
                    initMcPvTable(new String[]{"id","licheng","quxianchang","pianjiao","x","y"},InputModel.hjiList,GlobalConstant.MCPV);
                    mcDmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.MCPV,this));
                }else {
                    // TODO: 2022/5/14   复用initMcTable方法,就改参数就行
                    initMcPvTable(new String[]{"id","x","y"},InputModel.hcsList,GlobalConstant.MCCS);
                    mcDmBinding.save.setOnClickListener(v -> MyUtil.editDialog(GlobalConstant.MCCS,this));
                }

            }
        }
    }

    @Override
    protected void initData() {
    }


    @Override
    protected View getRootView() {
        data1 = ActivityUtil.getIntentData();
        data2 = ActivityUtil.getIntentSecondData();
        if (data1.equals(GlobalConstant.CIRCULAR_CURVE)){
            if (data2.equals(GlobalConstant.DECLINATION_METHOD)){
                ccDmBinding = ActivityResultCircularCurveDmBinding.inflate(getLayoutInflater());
                return ccDmBinding.getRoot();
            }else if (data2.equals(GlobalConstant.TANGENT_SUPPORT_METHOD)){
                ccSmBinding = ActivityResultCircularCurveSmBinding.inflate(getLayoutInflater());
                return ccSmBinding.getRoot();
            }else {
                ccDmBinding = ActivityResultCircularCurveDmBinding.inflate(getLayoutInflater());
                return ccDmBinding.getRoot();
            }
        }else {
            if (data2.equals(GlobalConstant.DECLINATION_METHOD)){
                mcDmBinding = ActivityResultModerationCurveDmBinding.inflate(getLayoutInflater());
                return mcDmBinding.getRoot();
            }else if (data2.equals(GlobalConstant.TANGENT_SUPPORT_METHOD)){
                mcSmBinding = ActivityResultModerationCurveSmBinding.inflate(getLayoutInflater());
                return mcSmBinding.getRoot();
            }else {
                mcDmBinding = ActivityResultModerationCurveDmBinding.inflate(getLayoutInflater());
                return mcDmBinding.getRoot();
            }
        }
    }

    // TODO: 2022/5/14  在新建计算代码后，来这个返回栈函数处调一下clear()函数，让只能看到一条记录
    /**
     * Called when the activity has detected the user's press of the back
     * key. The {@link #getOnBackPressedDispatcher() OnBackPressedDispatcher} will be given a
     * chance to handle the back button before the default behavior of
     * {@link Activity#onBackPressed()} is invoked.
     *
     * @see #getOnBackPressedDispatcher()
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        InputModel.resList.clear();
        InputModel.hpjList.clear();
        InputModel.yqxresList.clear();
        InputModel.hqxList.clear();
        InputModel.yjiList.clear();
        InputModel.hjiList.clear();
        InputModel.ycsList.clear();
        InputModel.hcsList.clear();
    }

    /**
     * 圆曲线偏角法布局设置
     * @param fieldNames 各列绑定字段
     * @param list 数据源
     */
    private void initCcDmTable(String[] fieldNames , List<?> list){
        ccDmBinding.textViewT.setText(InputModel.TS);
        ccDmBinding.textViewL.setText(InputModel.LS);
        ccDmBinding.textViewE.setText(InputModel.ES);
        ccDmBinding.textViewQ.setText(InputModel.QS);
        ccDmBinding.textViewZY.setText(InputModel.zyS);
        ccDmBinding.textViewQZ.setText(InputModel.qzS);
        ccDmBinding.textViewYZ.setText(InputModel.yzS);
        // 设置列数
        ccDmBinding.datagridview.setColunms(4);
        // 设置表头
        ccDmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number, R.string.mileage, R.string.curve_spacing
                , R.string.deflection_angle});
        // 绑定字段
        ccDmBinding.datagridview.setFieldNames(fieldNames);
        // 每个column占比
        ccDmBinding.datagridview.setColunmWeight(new float[]{1,1,1,1});
        //设置表头高度
        ccDmBinding.datagridview.setHeaderHeight(100);
        // 每个单元格包含控件
        ccDmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class, TextView.class});
        // 设置数据源
        ccDmBinding.datagridview.setDataSource(list);

        if (getDarkModeStatus(this)){
            ccDmBinding.svCcDm.setBackgroundColor(getResources().getColor(R.color.background_dark, getTheme()));
            ccDmBinding.datagridview.setHeaderBackgroundColorResId(R.color.Navy);
            ccDmBinding.datagridview.setRvContentColorResId(R.color.black1);
            ccDmBinding.datagridview.setDividerColor(R.color.Dick);
        }
        // 初始化表格
        ccDmBinding.datagridview.initDataGridView();
    }

    /**
     * 圆曲线切线支距法布局设置
     * @param fieldNames 各列绑定字段
     * @param list 数据源
     */
    private void initCcSmTable(String[] fieldNames , List<?> list){
        ccSmBinding.textViewT.setText(InputModel.TS);
        ccSmBinding.textViewL.setText(InputModel.LS);
        ccSmBinding.textViewE.setText(InputModel.ES);
        ccSmBinding.textViewQ.setText(InputModel.QS);
        ccSmBinding.textViewZY.setText(InputModel.zyS);
        ccSmBinding.textViewQZ.setText(InputModel.qzS);
        ccSmBinding.textViewYZ.setText(InputModel.yzS);
        // 设置列数
        ccSmBinding.datagridview.setColunms(5);
        // 设置表头
        ccSmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number,R.string.mileage, R.string.curve_length_of_each_pile_to_ZY_or_YZ, R.string.X
                , R.string.Y});
        // 绑定字段
        ccSmBinding.datagridview.setFieldNames(fieldNames);
        // 每个column占比
        ccSmBinding.datagridview.setColunmWeight(new float[]{1,1,2,1,1});
        //设置表头高度
        ccSmBinding.datagridview.setHeaderHeight(100);
        // 每个单元格包含控件
        ccSmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class,TextView.class, TextView.class, TextView.class, TextView.class});
        // 设置数据源
        ccSmBinding.datagridview.setDataSource(list);
        if (getDarkModeStatus(this)){
            ccSmBinding.svCcDm.setBackgroundColor(getResources().getColor(R.color.background_dark, getTheme()));
            ccSmBinding.datagridview.setHeaderBackgroundColorResId(R.color.Navy);
            ccSmBinding.datagridview.setRvContentColorResId(R.color.black1);
            ccSmBinding.datagridview.setDividerColor(R.color.Dick);
        }
        // 初始化表格
        ccSmBinding.datagridview.initDataGridView();
    }

    /**
     * 缓和曲线偏角法布局设置
     * @param fieldNames 各列绑定字段
     * @param list 数据源
     */
    private void initMcDmTable(String[] fieldNames , List<?> list){
        mcDmBinding.textViewT.setText(InputModel.TS);
        mcDmBinding.textViewE0.setText(InputModel.E0S);
        mcDmBinding.textViewL.setText(InputModel.LS);
        mcDmBinding.textViewHY.setText(InputModel.hyS);
        mcDmBinding.textViewZH.setText(InputModel.zhS);
        mcDmBinding.textViewQ.setText(InputModel.QS);
        mcDmBinding.textViewHZ.setText(InputModel.hzS);
        mcDmBinding.textViewQZ.setText(InputModel.qzS);
        mcDmBinding.textViewYH.setText(InputModel.yhS);
        // 设置列数
        mcDmBinding.datagridview.setColunms(4);
        // 设置表头
        mcDmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number, R.string.mileage, R.string.curve_spacing
                , R.string.deflection_angle});
        // 绑定字段
        mcDmBinding.datagridview.setFieldNames(fieldNames);
        // 每个column占比
        mcDmBinding.datagridview.setColunmWeight(new float[]{1,1,1,1});
        //设置表头高度
        mcDmBinding.datagridview.setHeaderHeight(100);
        // 每个单元格包含控件
        mcDmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class, TextView.class});
        // 设置数据源
        mcDmBinding.datagridview.setDataSource(list);
        if (getDarkModeStatus(this)){
            mcDmBinding.svCcDm.setBackgroundColor(getResources().getColor(R.color.background_dark, getTheme()));
            mcDmBinding.datagridview.setHeaderBackgroundColorResId(R.color.Navy);
            mcDmBinding.datagridview.setRvContentColorResId(R.color.black1);
            mcDmBinding.datagridview.setDividerColor(R.color.Dick);
        }
        // 初始化表格
        mcDmBinding.datagridview.initDataGridView();
    }

    /**
     * 缓和曲线切线支距法布局设置
     * @param fieldNames 各列绑定字段
     * @param list 数据源
     */
    private void initMcSmTable(String[] fieldNames , List<?> list){
        mcSmBinding.textViewT.setText(InputModel.TS);
        mcSmBinding.textViewE0.setText(InputModel.E0S);
        mcSmBinding.textViewL.setText(InputModel.LS);
        mcSmBinding.textViewHY.setText(InputModel.hyS);
        mcSmBinding.textViewZH.setText(InputModel.zhS);
        mcSmBinding.textViewQ.setText(InputModel.QS);
        mcSmBinding.textViewHZ.setText(InputModel.hzS);
        mcSmBinding.textViewQZ.setText(InputModel.qzS);
        mcSmBinding.textViewYH.setText(InputModel.yhS);
        // 设置列数
        mcSmBinding.datagridview.setColunms(5);
        // 设置表头
        mcSmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number,R.string.mileage,R.string.curve_length_of_each_pile_to_ZY_or_YZ, R.string.X
                , R.string.Y});
        // 绑定字段
        mcSmBinding.datagridview.setFieldNames(fieldNames);
        // 每个column占比
        mcSmBinding.datagridview.setColunmWeight(new float[]{1,1,1,1,1});
        //设置表头高度
        mcSmBinding.datagridview.setHeaderHeight(100);
        // 每个单元格包含控件
        mcSmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class, TextView.class, TextView.class});
        // 设置数据源
        mcSmBinding.datagridview.setDataSource(list);
        if (getDarkModeStatus(this)){
            mcSmBinding.svCcDm.setBackgroundColor(getResources().getColor(R.color.background_dark, getTheme()));
            mcSmBinding.datagridview.setHeaderBackgroundColorResId(R.color.Navy);
            mcSmBinding.datagridview.setRvContentColorResId(R.color.black1);
            mcSmBinding.datagridview.setDividerColor(R.color.Dick);
        }
        // 初始化表格
        mcSmBinding.datagridview.initDataGridView();
    }

    //确定好数据后，仿照上边载入数据，复用到的binding是 ccDmBinding
    /**
     * 圆曲线极坐标法和坐标放样法布局设置
     * @param fieldNames 各列绑定字段
     * @param list 数据源
     */
    private void initCcPVTable(String[] fieldNames , List<?> list ,int tag){
        ccDmBinding.textViewT.setText(InputModel.TS);
        ccDmBinding.textViewL.setText(InputModel.LS);
        ccDmBinding.textViewE.setText(InputModel.ES);
        ccDmBinding.textViewQ.setText(InputModel.QS);
        ccDmBinding.textViewZY.setText(InputModel.zyS);
        ccDmBinding.textViewQZ.setText(InputModel.qzS);
        ccDmBinding.textViewYZ.setText(InputModel.yzS);
        if (tag == GlobalConstant.CCPV){
            // 设置列数
            ccDmBinding.datagridview.setColunms(6);
            // 设置表头
            ccDmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number,R.string.mileage, R.string.curve_length_of_each_pile_to_ZY_or_YZ, R.string.deflection_angle, R.string.X, R.string.Y});
            // 每个column占比
            ccDmBinding.datagridview.setColunmWeight(new float[]{1,1,2,1,1,1});
            // 每个单元格包含控件
            ccDmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class, TextView.class, TextView.class, TextView.class});

        }else {
            // TODO: 2022/5/14 参照上边的if，填好坐标放样法表格对应的数据
            // 设置列数
            ccDmBinding.datagridview.setColunms(3);
            // 设置表头
            ccDmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number, R.string.X, R.string.Y});
            // 每个column占比
            ccDmBinding.datagridview.setColunmWeight(new float[]{1,1,1});
            // 每个单元格包含控件
            ccDmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class});
        }


        // 绑定字段
        ccDmBinding.datagridview.setFieldNames(fieldNames);

        //设置表头高度
        ccDmBinding.datagridview.setHeaderHeight(100);

        // 设置数据源
        ccDmBinding.datagridview.setDataSource(list);

        if (getDarkModeStatus(this)){
            ccDmBinding.svCcDm.setBackgroundColor(getResources().getColor(R.color.background_dark, getTheme()));
            ccDmBinding.datagridview.setHeaderBackgroundColorResId(R.color.Navy);
            ccDmBinding.datagridview.setRvContentColorResId(R.color.black1);
            ccDmBinding.datagridview.setDividerColor(R.color.Dick);
        }
        // 初始化表格
        ccDmBinding.datagridview.initDataGridView();
    }


    //确定好数据后，仿照上边载入数据，复用到的binding是 mcDmBinding
    /**
     * 缓和曲线极坐标法和坐标放样法布局设置
     * @param fieldNames 各列绑定字段
     * @param list 数据源
     */
    private void initMcPvTable(String[] fieldNames , List<?> list , int tag){
        mcDmBinding.textViewT.setText(InputModel.TS);
        mcDmBinding.textViewE0.setText(InputModel.E0S);
        mcDmBinding.textViewL.setText(InputModel.LS);
        mcDmBinding.textViewHY.setText(InputModel.hyS);
        mcDmBinding.textViewZH.setText(InputModel.zhS);
        mcDmBinding.textViewQ.setText(InputModel.QS);
        mcDmBinding.textViewHZ.setText(InputModel.hzS);
        mcDmBinding.textViewQZ.setText(InputModel.qzS);
        mcDmBinding.textViewYH.setText(InputModel.yhS);

        if (tag == GlobalConstant.MCPV){
            // 设置列数
            mcDmBinding.datagridview.setColunms(6);
            // 设置表头
            mcDmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number,R.string.mileage, R.string.curve_length_of_each_pile_to_ZY_or_YZ, R.string.deflection_angle, R.string.X
                    , R.string.Y});
            // 每个column占比
            mcDmBinding.datagridview.setColunmWeight(new float[]{1,1,2,1,1,1});
            // 每个单元格包含控件
            mcDmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class, TextView.class,TextView.class,TextView.class});
        }else {
            // TODO: 2022/5/14  参照上边的if，填好坐标放样法表格对应的数据
            // 设置列数
            mcDmBinding.datagridview.setColunms(3);
            // 设置表头
            mcDmBinding.datagridview.setHeaderContentByStringId(new int[]{R.string.pile_number,R.string.X, R.string.Y});
            // 每个column占比
            mcDmBinding.datagridview.setColunmWeight(new float[]{1,1,1});
            // 每个单元格包含控件
            mcDmBinding.datagridview.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class});
        }


        // 绑定字段
        mcDmBinding.datagridview.setFieldNames(fieldNames);

        //设置表头高度
        mcDmBinding.datagridview.setHeaderHeight(100);

        // 设置数据源
        mcDmBinding.datagridview.setDataSource(list);
        if (getDarkModeStatus(this)){
            mcDmBinding.svCcDm.setBackgroundColor(getResources().getColor(R.color.background_dark, getTheme()));
            mcDmBinding.datagridview.setHeaderBackgroundColorResId(R.color.Navy);
            mcDmBinding.datagridview.setRvContentColorResId(R.color.black1);
            mcDmBinding.datagridview.setDividerColor(R.color.Dick);
        }
        // 初始化表格
        mcDmBinding.datagridview.initDataGridView();
    }

}